package thread_pool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:11:49
 **/
public class PoolDemo22 {
    //实现1000个Date（时间类型）的格式化(线程不安全问题)
    //加锁，执行完停止
    //带来新的问题：排队执行的性能消耗问题

    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");

    public static void main(String[] args) {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,10,0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000)
        );

        for (int i = 1; i < 1001; i++) {
            final int finalI = i;


            executor.execute(new Runnable() {
                @Override
                public void run() {
                    Date date = new Date(finalI*1000);
                    myFormatTime(date);
                }
            });
        }
        executor.shutdown();
    }

    /**
     * 自定义格式化时间
     * @param date 时间
     */
    private static synchronized void myFormatTime(Date date) {
        String result = simpleDateFormat.format(date);
        System.out.println("时间"+result);
    }
}
